/*
 * Copyright (c) 2025 EasyAdmin
 * All rights reserved.
 *
 * This file is part of the [EasAdmin] project.
 * Unauthorized copying of this file, via any medium, is strictly prohibited.
 * Proprietary and confidential.
 *
 * Author: EasyAdmin ( 1073602@qq.com )
 * Website: https://www.xhxiao.com
 */
package com.easy.module.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.framework.cache.RedisCache;
import com.easy.framework.common.constant.Constants;
import com.easy.framework.common.exception.ServiceException;
import com.easy.framework.common.utils.ConvertUtil;
import com.easy.framework.common.utils.Func;
import com.easy.framework.common.utils.StringUtil;
import com.easy.module.system.entity.SysConf;
import com.easy.module.system.mapper.SysConfMapper;
import com.easy.module.system.service.ISysConfService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 参数配置表 Service业务
 *
 * @author EasyAdmin ( 1073602@qq.com )
 * @date 2025-08-19
 */
@RequiredArgsConstructor
@Service
public class SysConfServiceImpl extends ServiceImpl<SysConfMapper, SysConf> implements ISysConfService {

    private final RedisCache redisCache;


    /**
     * 新增配置
     *
     * @param dto 配置
     * @return 结果
     */
    @Override
    public boolean add(SysConf dto) {
        boolean sign = save(dto);
        if (sign) {
            redisCache.setCacheObject(getCacheKey(dto.getConfKey()), dto.getConfValue());
        }
        return sign;
    }

    /**
     * 修改配置
     *
     * @param dto 配置
     * @return 结果
     */
    @Override
    public boolean update(SysConf dto) {
        SysConf entity = getById(dto.getId());
        if (!StringUtil.equals(entity.getConfKey(), dto.getConfKey())) {
            redisCache.deleteObject(getCacheKey(entity.getConfKey()));
        }
        boolean sign = updateById(dto);
        if (sign) {
            redisCache.setCacheObject(getCacheKey(dto.getConfKey()), dto.getConfValue());
        }
        return sign;
    }

    /**
     * 批量删除配置
     *
     * @param ids 需要删除的配置主键
     * @return 结果
     */
    @Override
    public boolean deleteByIds(Long[] ids) {
        for (Long id : ids) {
            SysConf conf = getById(id);
            if (conf.getIsSystem()) {
                throw new ServiceException(String.format("内置配置【%1$s】不能删除 ", conf.getConfKey()));
            }
            removeById(id);
            redisCache.deleteObject(getCacheKey(conf.getConfKey()));
        }
        return true;
    }

    @Override
    public SysConf getByKey(String confKey) {
        return getOne(Wrappers.<SysConf>lambdaQuery().eq(SysConf::getConfKey, confKey), false);
    }

    /**
     * 设置cache key
     *
     * @param confKey 配置Key
     * @return 缓存键key
     */
    private String getCacheKey(String confKey) {
        return Constants.SYS_CONFIG_KEY + confKey;
    }

    @Override
    public String getValue(String confKey) {
        String confValue = ConvertUtil.toStr(redisCache.getCacheObject(getCacheKey(confKey)));
        if (Func.isNotEmpty(confValue)) {
            return confValue;
        }
        SysConf retConf = getByKey(confKey);
        if (Func.isNotEmpty(retConf)) {
            redisCache.setCacheObject(getCacheKey(confKey), retConf.getConfValue());
            return retConf.getConfValue();
        }
        return StringUtil.EMPTY;
    }

    /**
     * 配置Key是否存在
     *
     * @param conf 配置信息
     * @return 结果
     */
    @Override
    public boolean existByKey(SysConf conf) {
        SysConf info = getByKey(conf.getConfKey());
        if (Func.isNotEmpty(info) && !info.getId().equals(conf.getId())) {
            return true;
        }
        return false;
    }


    /**
     * 加载配置缓存数据
     */
    @Override
    public void loadConfCache() {
        List<SysConf> list = list();
        for (SysConf conf : list) {
            redisCache.setCacheObject(getCacheKey(conf.getConfKey()), conf.getConfValue());
        }
    }

    /**
     * 清空配置缓存数据
     */
    @Override
    public void clearConfCache() {
        Collection<String> keys = redisCache.keys(Constants.SYS_CONFIG_KEY + "*");
        redisCache.deleteObject(keys);
    }

    /**
     * 重置配置缓存数据
     */
    @Override
    public void resetConfCache() {
        clearConfCache();
        loadConfCache();
    }
}
